package org.jeecg.modules.app.service.impl;


import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.app.entity.Order;
import org.jeecg.modules.app.entity.OrderFood;
import org.jeecg.modules.app.mapper.OrderFoodMapper;
import org.jeecg.modules.app.mapper.OrderMapper;
import org.jeecg.modules.app.service.OrderService;
import org.jeecg.modules.app.vo.ConsumeVO;
import org.jeecg.modules.system.rule.OrderNumberRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 李圳川
 * @version 1.0
 * @date 2022/6/19 23:27
 * @content
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderFoodMapper orderFoodMapper;


    /**
     * 查询所有订单
     * @param customerId
     * @return
     */
    @Override
    public ArrayList<Order> findAllOrderByCustomerId(String customerId) {
        ArrayList<Order> orderArrayList = orderMapper.findAllOrderByCustomerId(customerId);
        return orderArrayList;
    }

    /**
     * 下单
     * @param order
     */
    @Override
    public String order(Order order) {
        OrderNumberRule orderNumberRule = new OrderNumberRule();
        Object value = orderNumberRule.execute(null, (JSONObject) JSONObject.toJSON(order));
        order.setSerialNum((String)value);
        orderMapper.insert(order);
        ArrayList<OrderFood> orderFoodArrayList = order.getFoodList();
        for(OrderFood food: orderFoodArrayList){

            food.setFoodorderId(order.getId());
            orderFoodMapper.insert(food);
        }
        return order.getId();
    }

    /**
     * 消费记录
     * @param id
     * @return
     */
    @Override
    public List<ConsumeVO> selectCustomerConsumeById(String id) {
        List<ConsumeVO> consumeVOList = orderMapper.selectCustomerConsumeById(id);
        return consumeVOList;
    }

    /**
     * 修改订单状态
     * @param id
     * @param status
     */
    @Override
    public void updateOrderById(String id,String status) {
        orderMapper.updateOrderById(id,status);
    }
}
